# encoding=gbk
import base64
import time

import detection
from flask import Flask, request

import detection_video

app = Flask(__name__)

@app.route('/')
def hello_world():
   return 'Hello World'


@app.route('/images',methods=['POST'])
def imagesDetection():
   paths = request.form.get('paths').replace('[','').replace(']','').replace('"','').split(',')
   res = detection.multipleDetection(paths)
   return res

@app.route('/image/<path>')
def imgDetection(path):
   path = base64.b64decode(path)
   res = detection.singleDetection(path)
   print(res)
   return str(res)

@app.route('/video/<path>')
def video_Detection(path):
   path = base64.b64decode(path).decode()
   print(path)
   start = time.time()
   data = detection_video.videoDetection(str(path))
   end = time.time()
   print("ʱ" + str(end-start) + "")
   superNSFW = []
   maybeNSFW = []
   for temp in data:
      probability = temp.get("probability")
      second = temp.get("second")
      if (float(str(probability.replace("%",""))) > 0.8):
         superNSFW.append(str("superNSFW occurs at:" + format(second,".2f") + "  probability is:" + probability))
      if (float(str(probability.replace("%",""))) > 0.6):
         maybeNSFW.append(str("maybeNSFW occurs at:" + format(second,".2f") + "  probability is:" + probability))
   res = {"code" : 1,"status" : 200,"maybeNSFW" : maybeNSFW,"superNSFW": superNSFW,"data" : data}
   return res

if __name__ == '__main__':
   app.run()
